﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;



namespace QuanLyNhaSach
{
    class KetNoiCSDL
    {
        public SqlConnection connect = new SqlConnection("server=.\\sqlexpress;database=QLNHASACH;Integrated security=true;");
        public SqlDataReader reader;//doi tuong doc csdl
        public KetNoiCSDL()
        {
        }
        //lay mot bang trong CSDL tra ve mot doi tuong DataTable
        public DataTable LayCSDL(String tempString)
        {            
            SqlDataAdapter dataAdapter = new SqlDataAdapter(tempString, connect);            
            DataTable dataTabble = new DataTable();            
            dataAdapter.Fill(dataTabble);
            return dataTabble;
        }

        //thuc thi mot cau lenh SQL
        public void thucthiCSDL(String stringCommand)
        {
            SqlCommand cmd = new SqlCommand(stringCommand, connect);
            if (connect.State == ConnectionState.Open)
            {
                connect.Close();
                connect.Open();
            }
            else
                connect.Open();
            cmd.ExecuteNonQuery();
            //connect.Dispose();
        }
        
        public SqlDataReader thihanhDataReader(String stringCommand)
        {
            SqlCommand cmd = new SqlCommand(stringCommand, connect);
            if (connect.State == ConnectionState.Open)
            {
                connect.Close();
                connect.Open();
            }
            else
                connect.Open();
            reader = cmd.ExecuteReader();
            //connect.Close();
            return reader;

        }

    }
}
